import { loginout } from "@/common";
import axios from "axios";
import { ElMessage } from "element-plus";

const instance = axios.create({
    baseURL: 'http://xue.cnkdl.cn:9082/mall-admin/',
    timeout: 5000
})
// 请求拦截器
instance.interceptors.request.use(config => {
    console.log("请求拦截器");
    let token = localStorage.getItem('token');
    if (token) {
        if (config.headers) {
            config.headers['Authorization'] = token;
        }
    }
    return config;
}, err => {
    Promise.reject(err);

})
// 响应拦截器
instance.interceptors.response.use(
    (response) => {
        console.log("响应拦截器")
        const res = response.data as ILoginResponse<any>;
        // 统一处理错误信息
        if(res.code==401){
            loginout();
            ElMessage.error("登录失败，请重新登录");
        }



        return response.data;

    }, err => {
        Promise.reject(err);

    })

export default instance;